package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Role;
import com.zy.service.RoleService;
import com.zy.vo.DataView;
import com.zy.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // TODO: 2023/6/12
//    @Autowired
//    private MenuService menuService;

    /**
     * 查询所有的角色带有分页，带有查询角色
     */
    @RequestMapping("/loadAllRole")
    @ResponseBody
    public DataView loadAllRole(RoleVo roleVo){
        IPage<Role> page = new Page<>(roleVo.getPage(), roleVo.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper<>();
        //判断前端传过来的name不能为空  并把name的值填到数据库列名为name的地方
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getRname()), "name", roleVo.getRname());
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getRemark()), "remark", roleVo.getRemark());
        roleService.page(page,queryWrapper);
        DataView dataView = new DataView(page.getTotal(), page.getRecords());
        dataView.setCode(0);
        return dataView;
    }

    /**
     * 添加角色
     */
    @RequestMapping("/addRole")
    @ResponseBody
    public DataView addRole(Role role){
        roleService.save(role);
        DataView dataView = new DataView();
        dataView.setMsg("添加角色成功");
        dataView.setCode(200);
        return  dataView;
    }

    /**
     * 删除角色
     */
    @RequestMapping("/deleteRole")
    @ResponseBody
    public DataView deleteRole(Role role){
        roleService.removeById(role.getId());
        DataView dataView = new DataView();
        dataView.setMsg("删除角色成功");
        dataView.setCode(200);
        return  dataView;
    }

    /**
     * 修改角色
     */
    @RequestMapping("/updateRole")
    @ResponseBody
    public DataView updateRole(Role role){
        roleService.saveOrUpdate(role);
        DataView dataView = new DataView();
        dataView.setMsg("修改角色成功");
        dataView.setCode(200);
        return  dataView;
    }

    /**
     * 初始化下拉列表具有的权限
     * 根据角色查询菜单
     * select mid from role_menu where rid = ?
     */
//    @RequestMapping("/initPermissionByRoleId")
//    @ResponseBody
//    public DataView initPermissionByRoleId(Integer roleId){
//        //1.把所有的菜单权限查出来
//        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//        List<Menu> allPermissions = menuService.list();
//        //2.根据角色rid【roleId】 查询所有的菜单mid
//        List<Integer> currentRolePermission =  roleService.queryAllPermissionByRid(roleId);
//        //3.rid mid  根据所有的id  查询菜单和角色的数据
//        List<Menu> menus = null;
//        //4.查询到  mid和 rid
//        if (currentRolePermission.size() > 0) {
//            queryWrapper.in("id",currentRolePermission);
//            menus = menuService.list(queryWrapper);
//        }else{
//            menus = new ArrayList<>();
//        }
//        //5.返回前台的格式，带有层级关系，  TreeNode
//        List<TreeNode> nodes = new ArrayList<>();
//        for (Menu allPermission : allPermissions){
//            String checkArr = "0";
//            for (Menu currentPermission:menus){
//                if (allPermission.getId().equals(currentPermission.getId())){
//                    checkArr = "1";
//                    break;
//                }
//            }
//            Boolean spread = (allPermission.getOpen()==null || allPermission.getOpen() ==1)?true:false;
//            nodes.add(new TreeNode(allPermission.getId(),allPermission.getPid(),allPermission.getTitle(),spread,checkArr));
//
//        }
//        DataView dataView = new DataView(nodes);
//        dataView.setCode(0);
//        return dataView;
//
//    }

    /**
     * 点击确认保存分配权限的时候
     * 插入数据库表【role_menu表】
     * var params = "rid="+data.id;
     *     console.log(params);
     *     $.each(permissionData,function (index, item) {
     *         params+="&ids="+item.nodeId;
     *     });
     */
    @RequestMapping("/saveRolePermission")
    @ResponseBody
    public DataView saveRolePermission(Integer rid,Integer[] mids){
        //1.根据rid删除之前的mid权限
        roleService.deleteRoleByRid(rid);
        if (mids != null && mids.length > 0) {
            for (Integer mid : mids){
                roleService.saveRoleMenu(rid,mid);
            }
        }
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("菜单权限分配成功！");
        return dataView;
    }



}
